package com.tensquare.user.controller;

import java.util.HashMap;
import java.util.Map;

import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import com.tensquare.user.util.UserConstant;
import com.tensquare.util.JwtUtil;
import com.tensquare.util.NotNullHelps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.tensquare.user.model.User;
import com.tensquare.user.service.UserService;


@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;

	private Result result = new Result(true, StatusCode.OK);

	@Autowired
	private RedisTemplate redisTemplate ;

	@Autowired
	private JwtUtil jwtUtil ;

	private String message ;

	@RequestMapping(value="/{id}",method= RequestMethod.DELETE)
	public Result delete(@PathVariable String id ){
		userService.deleteById(id);
		return new Result(true,StatusCode.OK,"删除成功");
	}

	@RequestMapping(value="/{id}",method= RequestMethod.PUT)
	public Result update(@RequestBody User user, @PathVariable String id ){
		user.setId(id);
		userService.update(user);
		return new Result(true,StatusCode.OK,"修改成功");
	}

	@RequestMapping(value="/{userId}/{friendId}/{count}",method= RequestMethod.PUT)
	public void updateFanAndFollowCount(@PathVariable  String userId , @PathVariable String friendId,@PathVariable Integer count){
		userService.updateFanAndFollowCount(count ,userId, friendId);
	}


	@RequestMapping(method=RequestMethod.POST)
	public Result add(@RequestBody User user  ){
		userService.saveUser(user);
		return new Result(true,StatusCode.OK,"增加成功");
	}


	@RequestMapping(value="/search",method = RequestMethod.POST)
	public Result findSearch( @RequestBody Map searchMap){
		return new Result(true,StatusCode.OK,"查询成功",userService.findSearch(searchMap));
	}

	@RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
	public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
		Page<User> pageList = userService.findSearch(searchMap, page, size);
		return  new Result(true,StatusCode.OK,"查询成功",  new PageResult<User>(pageList.getTotalElements(), pageList.getContent()) );
	}

	@RequestMapping(value = "/sendsms/{mobile}",method = RequestMethod.POST)
	public Result sendSMS(@PathVariable String mobile){
		userService.sendSMS(mobile);
		result.setMessage("短信发送成功");
		return result ;
	}

	// 741880
	@RequestMapping(value = "/register/{code}",method = RequestMethod.POST)
	public Result register(@PathVariable String code , @RequestBody User user){
		String mobile = user.getMobile();
		result = new Result(false, StatusCode.ERROR);
		if (NotNullHelps.checkNotNull(mobile, code) == false) {
			message = UserConstant.message_exception;
		} else {
			String redisKey = UserConstant.redis_code_prefix + mobile;
			String redisCode = (String) redisTemplate.opsForValue().get(redisKey);
			if (code.equals(redisCode) == false) {
				message = "验证码有误";
			} else {
				result = new Result(true, StatusCode.OK);
				userService.saveUser(user);
				message = "注册用户成功";
			}
		}
		result.setMessage(message);
		return result ;
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public Result loginUser(@RequestBody User user) {
		user = userService.loginUser(user);
		if (NotNullHelps.isNotNull(user) == false) {
			result = new Result(false, StatusCode.LOGINERROR);
			message = "[user]--登录异常";
		} else {
			String jwtToken = jwtUtil.createJWT(user.getId(), user.getMobile(), "user");
			Map<String, String> map = new HashMap<>();
			message = "登录成功--[user]";
			map.put("token", jwtToken);
			map.put("role", "user");
			result.setData(map);
		}
		result.setMessage(message);
		return result ;
	}

	@RequestMapping(method= RequestMethod.GET)
	public Result findAll(){
		return new Result(true, StatusCode.OK,"查询成功",userService.findAll());
	}


	@RequestMapping(value="/{id}",method= RequestMethod.GET)
	public Result findById(@PathVariable String id){
		return new Result(true,StatusCode.OK,"查询成功",userService.findById(id));
	}


	
}
